#!/usr/bin/perl

###################################################################################### 
#                                                                                    #
# check_cpnr_process                                                                 #
#                                                                                    #
# Check Cisco Prime Network Registrar processes                                      #
#                                                                                    #
# Create by kresimir.petrovic@gmail.com for moniring CPNR modules                    #
#                                                                                    #
#                                                                                    #
# Nagios::Plugin perl module is required                                             #
#                                                                                    #
###################################################################################### 

package main;

our $VERSION = '1.0.5';

use strict;
use POSIX;

use Nagios::Plugin;
use Nagios::Plugin::Getopt;
use Nagios::Plugin::Threshold;

if ( !caller ) {
    main();
}

sub main {
	
	my ($result_text, $check_result, $check_text, $error_level);
	my ($plugin, $arg_opts);
	my (@processes);
	my %actions = ( snmp		=> \&check_cnrsnmp_process,
					tftp		=> \&check_tftp_process,
					ccmsrv		=> \&check_ccmsrv_process, 
					ric			=> \&check_cnrric_process, 
					dhcp		=> \&check_dhcp_process, 
					dns			=> \&check_dns_process, 
					webui		=> \&check_cnrwebui_process
				  );

	$plugin = Nagios::Plugin->new( shortname => 'CPNR Process'); 
	$arg_opts = parse_nagios_plugin_options();	


	# Parse process names, divided them by commas
	@processes = split (/,/,$arg_opts->processes());

	if ($arg_opts->processes() =~ m/all/ or  !@processes)
	{
		# Run all checks
		print "[DEBUG] Running all checks!!!\n" if $arg_opts->debug();
		@processes = keys %actions;
	}
	# For which processes to run checks?
	else
	{
		print "[DEBUG] \"@processes\" specified in commad line\n" if $arg_opts->debug();
	}

	$check_result=-1;
	$error_level = 'UNKNOWN';

	# Go and run checks
	foreach my $action(@processes)
	{
		print "	[DEBUG] Running check for [$action]\n" if $arg_opts->debug();
		if (defined  $actions{$action})
		{
			($check_result, $check_text) = $actions{$action}->();
		}
		else
		{
			print "		[DEBUG] Check \"$action\" not defined, skipping!!!\n" if $arg_opts->debug();
		}
		# Process is dead, raise the alarm
		if ($check_result == 0)
		{
			# Clear error text if error level was OK
			$result_text = '' if ($error_level eq 'OK');
			$error_level = 'CRITICAL';
			print "		[DEBUG] Check for \"$action\" is $error_level: $check_text\n" if $arg_opts->debug();

			# Collect error text from all checks together
			if (!defined $result_text or $result_text eq '')
			{
				$result_text = $check_text;
			} 
			else
			{
				$result_text = $result_text.", ".$check_text;
			}
		# Process is OK and no other proceses before him raised the alarm
		} elsif ($check_result >=1 and $error_level ne 'CRITICAL')
		{
			$error_level = 'OK';
			$result_text = 'Everything running like it should.';
			print "		[DEBUG] Check for \"$action\" is $error_level: $action is working OK\n" if $arg_opts->debug();
		}
	}
	# Exit with proper error level and error text
	$plugin->nagios_exit( $error_level, $result_text );
}

sub check_ccmsrv_process {
	my ($retval);

	$retval = `ps -ef | grep nwreg | grep ccmsrv | grep -v grep | wc -l`;
	return ($retval,'CCMSRV not behaving properly');
}

sub check_cnrwebui_process {
	my ($retval);

	$retval = `ps -ef | grep nwreg | grep webui\.jar | grep -v grep | wc -l`;
	return ($retval,'Web-GUI not behaving properly');
}

sub check_cnrric_process {
	my ($retval);

	$retval = `ps -ef | grep nwreg | grep RICServer | grep -v grep | wc -l`;
	return ($retval,'RIC server not behaving properly');
}

sub check_dhcp_process {
	my ($retval);

	$retval = `ps -ef | grep nwreg | grep dhcp | grep -v grep | wc -l`;
	return ($retval, 'DHCP not behaving properly');
}

sub check_dns_process {
	my ($retval);

	$retval = `ps -ef | grep nwreg | grep dns | grep -v grep | wc -l`;
	return ($retval, 'DNS not behaving properly');
}

sub check_cnrsnmp_process {
	my ($retval);

	$retval = `ps -ef | grep nwreg | grep cnrsnmp | grep -v grep | wc -l`;
	return ($retval, 'SNMP not behaving properly');
}

sub check_tftp_process {
	my ($retval);

	$retval = `ps -ef | grep nwreg | grep tftp | grep -v grep | wc -l`;
	return ($retval, 'TFTP not behaving properly');
}

sub parse_nagios_plugin_options()
{
	my ($options);

	$options = Nagios::Plugin::Getopt->new( 
		usage => "Usage: %s [ -v|--verbose ]  [ -P|--process <all|dhcp,dns,snmp,tftp,webui,ccmsrv,ric> ] [ -t <timeout> ] ",
		version => $VERSION,
		blurb   => 'Monitor CPNR processes. If specific process is dead, raise cirtical alarm. If specific process is not specified, all CPNR processes are monitored',
		extra   => 'Created by: Kresimir Petrovic kresimir.petrovic@gmail.com',
		url     => 'http://code.google.com/p/nagios-cpnr-monitoring/',
		timeout => 15
	);

    $options->arg(
		spec     => 'processes|P=s',
		help     => 'processes to monitor',
		required => 0
    );

    $options->arg(
		spec     => 'debug',
		help     => 'debugging output',
		required => 0
    );
     
    $options->arg(
		spec     => 'noperfdata|n',
		help     => 'no perfdata to output',
		required => 0
    );

    $options->getopts();
	return $options;
}
